import request from '@/utils/request.js'
import { useRolePrefix } from '@/hooks/useRolePrefix.js'

//* 用户登录
export function userLogin(role, data) {
  let rolePrefix = useRolePrefix(role)
  return request({
    url: `/${rolePrefix}/allow/login`,
    method: 'post',
    data
  })
}

//* 更新头像（base64，已废弃）
export function updateAvatar(role, data) {
  let rolePrefix = useRolePrefix(role)
  return request({
    url: `/${rolePrefix}/update/avatar`,
    method: 'post',
    data
  })
}

//* 根据 id 查找用户
export function getUserById(role, id) {
  let rolePrefix = useRolePrefix(role)
  return request({
    url: `/${rolePrefix}/id/${id}`
  })
}

//* 根据 role 查找角色菜单
export function getMenusByRole(role) {
  return request({
    url: `/menus/${role}`
  })
}

//* 根据 编号 查找管理员和教师
export function getUserByIdentify(role, identify) {
  let rolePrefix = useRolePrefix(role)
  return request({
    url: `/${rolePrefix}/identify/${identify}`
  })
}

//* 根据 手机号 查找用户
export function getUserByPhone(role, phone) {
  let rolePrefix = useRolePrefix(role)
  return request({
    url: `/${rolePrefix}/phone/${phone}`
  })
}

//* 根据 id 修改密码
export function updateUserPassWord(role, data) {
  const rolePrefix = useRolePrefix(role)
  return request({
    url: `/${rolePrefix}/update/password`,
    method: 'post',
    data
  })
}

//* 根据 id 修改个人信息
export function updateUserInfo(role, data) {
  const rolePrefix = useRolePrefix(role)
  return request({
    url: `/${rolePrefix}/update/info`,
    method: 'post',
    data
  })
}

//* 上传头像
export function uploadAvatar(role, data) {
  const rolePrefix = useRolePrefix(role)
  return request({
    url: `/${rolePrefix}/upload/avatar`,
    method: 'post',
    data
  })
}

//* 获取用户列表
export function getUsers(options) {
  const { role, page_num, page_size } = options
  // console.log(role, page_num, page_size);
  const rolePrefix = useRolePrefix(role)
  return request({
    url: `/${rolePrefix}/${rolePrefix}s?page_num=${page_num}&page_size=${page_size}`
  })
}

//* 根据 id 删除用户
export function deleteUserById(role, id) {
  const rolePrefix = useRolePrefix(role)
  return request({
    url: `${rolePrefix}/id/${id}`,
    method: 'delete'
  })
}

//* 创建用户
export function createUser(role, data) {
  const rolePrefix = useRolePrefix(role)
  return request({
    url: `${rolePrefix}/allow/register`,
    method: 'post',
    data
  })
}

//* 修改用户状态
export function updateState(role, id) {
  const rolePrefix = useRolePrefix(role)
  return request({
    url: `${rolePrefix}/update/state/${id}`,
    method: 'put'
  })
}


/**
 * 用户动态请求查询
 * @param {Object} options （role角色、label查询方式、value数据）
 * @returns 
 */
export function userSearch(options) {
  const { role, label, value } = options
  let rolePrefix = useRolePrefix(role)
  // 如果使用 name 模糊搜索，则 data 参数应该包含 name、page_num，page_size
  if (label === 'name' || label === 'major' || label === 'college') {
    return request({
      url: `/${rolePrefix}/${label}`,
      method: 'post',
      data: {
        [label]: value,
        page_num: options.page_num,
        page_size: options.page_size
      }
    })
  } else {
    return request({
      url: `/${rolePrefix}/${label}/${value}`
    })
  }
}

//* 获取全部数据（用户导出excel数据）
export function getAllUsers(role) {
  let rolePrefix = useRolePrefix(role)
  return request({
    url:`/${rolePrefix}/${rolePrefix}s`
  })
}

//* 上传excel表格
export function uploadExcel(role,data) {
  let rolePrefix = useRolePrefix(role)
  return request({
    url: `/${rolePrefix}/upload/excel`,
    method: 'post',
    data
  })
}
